package com.fau.cm.action.index;


import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalDailyTaskSta;
import com.fau.cm.util.DateUtil;
import com.fau.cm.util.FileNameEncoderUtil;
import com.fau.cm.util.SystemConfig;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 任务统计
 * @author dangjh
 * @datetime 2014年11月28日 下午4:59:22
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class TaskStaAction extends PublicAction implements ModelDriven<MedicalDailyTaskSta> {

	private static final long serialVersionUID = -3767253158532129045L;

	private MedicalDailyTaskSta task = new MedicalDailyTaskSta();
	
	/**
	 * @desc 实例化组任务统计界面
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:53:44
	 */
	@Action(value = "init_task_group_sta_list", results = {@Result(name = SUCCESS, location = "init_task_group_sta_list.jsp")})
	public String initTaskGroupStaList() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载组任务统计数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:56:16
	 */
	@Action(value = "task_group_sta_view")
	public String taskGroupStaView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		task.setHospitalId(user.getEnterpriseId());
		if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
			task.setGroupId(user.getGroup().getId());
		}
		list = this.baseAllService.getStatisticsService().findGroupTasksStatistics(task);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 导出组任务统计信息excel
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月28日 下午11:47:22
	 */
	@Action(value = "exports_group_task_info")
	public String exportsGroupTasks() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			task.setHospitalId(user.getEnterpriseId());
			if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
				task.setGroupId(user.getGroup().getId());
			}
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"本组任务统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().exportGroupTaskStatistics(task);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 实例化中心任务统计界面
	 * @return SUCCESS
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:53:44
	 */
	@Action(value = "init_task_center_sta_list", results = {@Result(name = SUCCESS, location = "init_task_center_sta_list.jsp")})
	public String initTaskCenterStaList() {
		return SUCCESS;
	}
	
	/**
	 * @desc 加载中心任务统计数据
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月28日 下午1:56:16
	 */
	@Action(value = "task_center_sta_view")
	public String taskCenterStaView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		task.setHospitalId(user.getEnterpriseId());
		if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
			task.setCenterId(user.getGroup().getPlatform().getId());
		}
		list = this.baseAllService.getStatisticsService().findPlatformTasksStatistics(task);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 导出中心任务统计信息excel
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月28日 下午11:47:22
	 */
	@Action(value = "exports_center_task_info")
	public String exportsCenterTasks() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			task.setHospitalId(user.getEnterpriseId());
			if(SystemConfig.CM_SYSTEM_TYPE.equals(user.getDeptType())) {
				task.setCenterId(user.getGroup().getPlatform().getId());
			}
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"中心任务统计信息"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getStatisticsService().exportPlatformTaskStatistics(task);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalDailyTaskSta getModel() {
		return task;
	}

}
